/**    
 * @{#} ShareUtils.java Create on 2014-7-2 上午10:26:21    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.evan.common.share;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import cn.skinapp.R;

import com.evan.common.utils.CommonUtility;
import com.sina.weibo.sdk.api.BaseMediaObject;
import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMessage;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler.Response;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.utils.Utility;

public class ShareWeiboUtils implements Response {

	private static ShareWeiboUtils shareUtils;
	private Context context;

	/** 微博微博分享接口实例 */
	private IWeiboShareAPI mWeiboShareAPI = null;
	private static String APP_ID;

	public static void setWeiboAPPID(String app_id) {
		APP_ID = app_id;
	}

	public ShareWeiboUtils(Context context) {
		this.context = context;
		mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(context, APP_ID);
		mWeiboShareAPI.registerApp();
	}

	public static ShareWeiboUtils getInstance(Context context) {
		if (CommonUtility.isNull(shareUtils)) {
			shareUtils = new ShareWeiboUtils(context);
		}
		return shareUtils;
	}

	/**
	 * 创建文本消息对象。
	 * 
	 * @return 文本消息对象。
	 */
	public TextObject getTextObj(String text) {
		TextObject textObject = new TextObject();
		textObject.text = text;
		return textObject;
	}

	/**
	 * 创建图片消息对象。
	 * 
	 * @return 图片消息对象。
	 */
	public ImageObject getImageObj(Drawable drawable) {
		ImageObject imageObject = new ImageObject();
		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		imageObject.setImageObject(bitmapDrawable.getBitmap());
		return imageObject;
	}

	/**
	 * 创建多媒体（网页）消息对象。
	 * 
	 * @return 多媒体（网页）消息对象。
	 */
	public WebpageObject getWebpageObj(Object title, Object description,
			Bitmap bitmap, String url) {
		WebpageObject mediaObject = new WebpageObject();
		mediaObject.identify = Utility.generateGUID();
		mediaObject.title = title.toString();
		mediaObject.description = description.toString();

		// 设置 Bitmap 类型的图片到视频对象里
		mediaObject.setThumbImage(bitmap);
		mediaObject.actionUrl = url;
		mediaObject.defaultText = "Webpage 默认文案";
		return mediaObject;
	}

	// /**
	// * 创建多媒体（音乐）消息对象。
	// *
	// * @return 多媒体（音乐）消息对象。
	// */
	// private MusicObject getMusicObj() {
	// // 创建媒体消息
	// MusicObject musicObject = new MusicObject();
	// musicObject.identify = Utility.generateGUID();
	// musicObject.title = mShareMusicView.getTitle();
	// musicObject.description = mShareMusicView.getShareDesc();
	//
	// // 设置 Bitmap 类型的图片到视频对象里
	// musicObject.setThumbImage(mShareMusicView.getThumbBitmap());
	// musicObject.actionUrl = mShareMusicView.getShareUrl();
	// musicObject.dataUrl = "www.weibo.com";
	// musicObject.dataHdUrl = "www.weibo.com";
	// musicObject.duration = 10;
	// musicObject.defaultText = "Music 默认文案";
	// return musicObject;
	// }
	//
	// /**
	// * 创建多媒体（视频）消息对象。
	// *
	// * @return 多媒体（视频）消息对象。
	// */
	// private VideoObject getVideoObj() {
	// // 创建媒体消息
	// VideoObject videoObject = new VideoObject();
	// videoObject.identify = Utility.generateGUID();
	// videoObject.title = mShareVideoView.getTitle();
	// videoObject.description = mShareVideoView.getShareDesc();
	//
	// // 设置 Bitmap 类型的图片到视频对象里
	// videoObject.setThumbImage(mShareVideoView.getThumbBitmap());
	// videoObject.actionUrl = mShareVideoView.getShareUrl();
	// videoObject.dataUrl = "www.weibo.com";
	// videoObject.dataHdUrl = "www.weibo.com";
	// videoObject.duration = 10;
	// videoObject.defaultText = "Vedio 默认文案";
	// return videoObject;
	// }
	//
	// /**
	// * 创建多媒体（音频）消息对象。
	// *
	// * @return 多媒体（音乐）消息对象。
	// */
	// private VoiceObject getVoiceObj() {
	// // 创建媒体消息
	// VoiceObject voiceObject = new VoiceObject();
	// voiceObject.identify = Utility.generateGUID();
	// voiceObject.title = mShareVoiceView.getTitle();
	// voiceObject.description = mShareVoiceView.getShareDesc();
	//
	// // 设置 Bitmap 类型的图片到视频对象里
	// voiceObject.setThumbImage(mShareVoiceView.getThumbBitmap());
	// voiceObject.actionUrl = mShareVoiceView.getShareUrl();
	// voiceObject.dataUrl = "www.weibo.com";
	// voiceObject.dataHdUrl = "www.weibo.com";
	// voiceObject.duration = 10;
	// voiceObject.defaultText = "Voice 默认文案";
	// return voiceObject;
	// }

	public void share(BaseMediaObject object) {

		if (mWeiboShareAPI.isWeiboAppInstalled()) {
			if (mWeiboShareAPI.isWeiboAppSupportAPI()) {
				WeiboMessage weiboMessage = new WeiboMessage();
				WeiboMultiMessage msg = new WeiboMultiMessage();
				msg.mediaObject = object;
				// 2. 初始化从第三方到微博的消息请求
				SendMultiMessageToWeiboRequest req = new SendMultiMessageToWeiboRequest();
				SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
				// 用transaction唯一标识一个请求
				req.transaction = String.valueOf(System.currentTimeMillis());
				// req.message = weiboMessage;
				req.multiMessage = msg;
				// 3. 发送请求消息到微博，唤起微博分享界面
				mWeiboShareAPI.sendRequest(req);

			} else {
				CommonUtility.toastLong(context,
						R.string.weibosdk_demo_not_support_api_hint);
			}
		}
	}

	@Override
	public void onResponse(BaseResponse arg0) {
		Log.i("MyLog", arg0.errMsg + "");
	}

}
